/*
 *		 Geek Textarea Editor 
 *		中国极客智能代码编辑器
 *
 *	Copyright (C) UX-Contry(www.hcmo.pw) 2016-2017
 *	优新世纪工作室-恋空  编写
 *	
 */
 
 window.GeekEditor=
 {
	init: function() {
	   if(GeekEditor.inited==true)
		   return;
	   GeekEditor.inited=true;
	   GeekEditor.lastKey="";
       var ta = document.createElement('textarea');
       ta.value = "\n";
       if(ta.value.length==2){
          GeekEditor.newLine = "\r\n";
       } else {
          GeekEditor.newLine = "\n";
       }
	   GeekEditor.InsertTabs.asm[3].end = GeekEditor.newLine;
	   if(__Geekkit!=undefined)	{
		   $$("ObjectManager").registObject("GeekEditor",window.GeekEditor);
		   console.log("load GeekEditor object to Geekkit!");
		}
	   console.log("Geek Editor Initiate Finish!");
	},
	createEditor: function(tarea,tabIndex) {
		if(tarea.type!="textarea"){
			return;
		}
		tarea.tabIndex= tabIndex || 0;
		//tarea.title="GeekEditor智能编辑器，具体访问http://www.hcmo.pw/geekeditor.php";
		tarea.title="";
		tarea.defaultValue="";
		tarea.onkeypress=GeekEditor.eventHandler.onkeypress;
		return tarea;
	},
	eventHandler:{
		onkeydown:function(e) {
			
		},
		onkeypress:function(e) {
			//alert('hello');
			var keycode;
			if(window.event)	 // IE
			{
			  keycode = e.keyCode;
			}
			else				 // Netscape/Firefox/Opera
			{
			  keycode = e.which;
			}
			keychar = String.fromCharCode(keycode);
			if((GeekEditor.lastKey=="\\" && keychar=="\"") || (GeekEditor.lastKey=="\\" && keychar=="\'")){
				GeekEditor.lastKey=keychar;
				return true;
			}
			for (var i = 0; i < GeekEditor.autoInsertTab.length; i++) {
				var item = GeekEditor.autoInsertTab[i];
				if(item.start==keychar) {
					//var lastnum=GeekEditor.fun.getPos(e.target);
					GeekEditor.fun.insertText(e.target,item.start + item.end);
					//GeekEditor.fun.setPos(e.target,lastnum+1);
					GeekEditor.lastKey=keychar;
					return false;
				}
			}
			GeekEditor.lastKey=keychar;
			return true;
		},
		onkeyup:function(e) {
			
		}
	},
	fun: {
		insertText:function(obj,str) {
			if (document.selection) {
				var sel = document.selection.createRange();
				sel.text = str;
			} else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
				var startPos = obj.selectionStart,
					endPos = obj.selectionEnd,
					cursorPos = startPos,
					tmpStr = obj.value;
				obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
				cursorPos += str.length;
				obj.selectionStart = obj.selectionEnd = cursorPos;
			} else {
				obj.value += str;
			}
		}/*,
		getPos: function(obj){ 
			var result = 0; 
			if(obj.selectionStart){ 	//IE以外 
				result = obj.selectionStart
			}else{ 						//IE 
				var rng; 
				rng = event.srcElement.createTextRange(); 
				rng.moveToPoint(event.x,event.y); 
				rng.moveStart("character",-event.srcElement.value.length); 
				result = rng.text.length; 
			} 
			return result; 
		},
		setPos: function (obj,num) {     
			obj.focus();
			var r =obj.createTextRange();     
			r.moveStart('character',num);     
			r.collapse(true);     
			r.select();     
		} */
		,moveEnd: function(obj){
			obj.focus();
			var len = obj.value.length;
			if (document.selection) {
				var sel = obj.createTextRange();
				sel.moveStart('character',len);
				sel.collapse();
				sel.select();
			} else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
				obj.selectionStart = obj.selectionEnd = len;
			}
		}
		// add else function
	},
	autoInsertTab:[],
	InsertTabs:{
		/** default **/
		default:[
			{start:"\"",end:"\""},
			{start:"'",end:"'"},
			{start:"(",end:")"},
			{start:"[",end:"]"},
			{start:"{",end:"}"}
		],
		/** No auto insert text**/
		noinsert:[],
		/** 中文输入 **/
		chinese:[
			{start:"“",end:"”"},
			{start:"（",end:"）"},
			{start:"《",end:"》"}
		],
		/** HTML **/
		html:[
			{start:"\"",end:"\""},
			{start:"'",end:"'"},
			{start:"(",end:")"},
			{start:"[",end:"]"},
			{start:"<",end:">"},
			{start:"{",end:"}"}
		],
		/** PHP **/
		php:[
			{start:"\"",end:"\""},
			{start:"'",end:"'"},
			{start:"(",end:")"},
			{start:"[",end:"]"},
			{start:"<",end:">"},
			{start:"{",end:"}"}
		],
		/** XML **/
		xml:[
			{start:"\"",end:"\""},
			{start:"'",end:"'"},
			{start:"(",end:")"},
			{start:"[",end:"]"},
			{start:"<",end:">"},
			{start:"{",end:"}"}
		],
		/** Windows INI File **/
		ini:[
			{start:"[",end:"]"}
		],
		/** CSS Style**/
		css:[
			{start:"\"",end:"\""},
			{start:"'",end:"'"},
			{start:"(",end:")"},
			{start:"[",end:"]"},
			{start:":",end:";"},
			{start:"{",end:"}"}
		],
		/** Java Script**/
		js:[
			{start:"\"",end:"\""},
			{start:"'",end:"'"},
			{start:"(",end:")"},
			{start:"[",end:"]"},
			{start:"{",end:"}"}
		],
		/** Java **/
		java:[
			{start:"\"",end:"\""},
			{start:"'",end:"'"},
			{start:"(",end:")"},
			{start:"<",end:">"},
			{start:"[",end:"]"},
			{start:"{",end:"}"}
		],
		/** JSON **/
		json:[
			{start:"\"",end:"\""},
			{start:"'",end:"'"},
			{start:"(",end:")"},
			{start:"[",end:"]"},
			{start:"<",end:">"},
			{start:"{",end:"}"}
		],
		/** C/C++/C#**/
		c:[
			{start:"\"",end:"\""},
			{start:"'",end:"'"},
			{start:"(",end:")"},
			{start:"<",end:">"},
			{start:"[",end:"]"},
			{start:"{",end:"}"}
		],
		/** 汇编 **/
		asm:[
			{start:"\"",end:"\""},
			{start:"'",end:"'"},
			{start:"[",end:"]"},
			{start:":",end:"\n"}
		],
		/** Visual Basic**/
		vb:[
			{start:"\"",end:"\""},
			{start:"'",end:"'"},
			{start:"(",end:")"},
		],
		/** 易语言 **/
		e:[
			{start:"\"",end:"\""},
			{start:"'",end:"'"},
			{start:"(",end:")"},
			{start:"[",end:"]"}
		],
	}
 };
 